﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockCommon;
using MockDataTransferObject;
using MockBusiness;
using System.Globalization;

namespace Presentation.AdminPresentation
{
    public partial class UserProfile : System.Web.UI.Page
    {
        private Button _saveButton;
        private Button _backButton;
        private Button _editButton;
        private UserBusiness _userBusiness;
        private ConsultantBusiness _consultantBusiness;
        private ClientBusiness _clientBusiness;
        private ContractorBusiness _contractorBusiness;
        private Label _warningLabel;
        private Label _errorLabel;
        private Label _successLabel;

        protected void Page_Init(object sender, EventArgs e)
        {
            try
            {
                _userBusiness = UserBusiness.RetrieveInstance();
                _consultantBusiness = ConsultantBusiness.RetrieveInstance();
                _clientBusiness = ClientBusiness.RetrieveInstance();
                _contractorBusiness = ContractorBusiness.RetrieveInstance();
                _saveButton = (Button)EditUserProfile.FindControl("SaveButton");
                _backButton = (Button)EditUserProfile.FindControl("BackButton");
                _editButton = (Button)ViewUserProfile.FindControl("EditButton");
                _warningLabel = (Label)Master.FindControl("WarningLabel");
                _errorLabel = (Label)Master.FindControl("ErrorLabel");
                _successLabel = (Label)Master.FindControl("SuccessLabel");
                _saveButton.Click += new EventHandler(SaveButton_Click);
                _backButton.Click += new EventHandler(BackButton_Click);
                _editButton.Click += new EventHandler(EditButton_Click);
            }
            catch (MockException exception)
            {
                Application["error"] = exception;
                Response.Redirect("../Error.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] == null)
            {
                Response.Redirect("../LogOn.aspx");
            }
            else
            {
                User user = (User)Session["user"];
                if (user.Role.RoleName != "Admin")
                {
                    Server.Transfer("../NotAuthorized.aspx");
                }
            }
            Loading(true);
            if (IsPostBack)
            {
                _warningLabel.Text = "";
                _errorLabel.Text = "";
                _successLabel.Text = "";
            }
        }

        protected void EditButton_Click(object sender, EventArgs e)
        {
            Loading(false);
            ProfilePanel.Visible = false;
            EditProfilePanel.Visible = true;
        }

        protected void BackButton_Click(object sender, EventArgs e)
        {
            Loading(true);
            ProfilePanel.Visible = true;
            EditProfilePanel.Visible = false;
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            Update();
            Loading(true);
            ProfilePanel.Visible = true;
            EditProfilePanel.Visible = false;
        }

        private void Loading(bool view)
        {
            try
            {
                object queryUsername = Request.QueryString["uname"];
                object queryRole = Request.QueryString["rname"];
                AbstractEntity source = null;
                if (queryUsername != null && queryRole != null)
                {
                    string username = queryUsername.ToString();
                    string rolename = queryRole.ToString().ToLower(CultureInfo.CurrentCulture);

                    if (rolename == "admin")
                    {
                        source = _userBusiness.GetByUserName(username);
                    }
                    else if (rolename == "contractor")
                    {
                        source = _contractorBusiness.GetByUserName(username);
                    }
                    else if (rolename == "client")
                    {
                        source = _clientBusiness.GetByUserName(username);
                    }
                    else if (rolename == "consultant")
                    {
                        source = _consultantBusiness.GetByUserName(username);
                    }
                }

                if (source != null)
                {
                    if (view)
                    {
                        ViewUserProfile.Binding(source);
                    }
                    else
                    {
                        EditUserProfile.Binding(source);
                    }
                }
                else
                {
                    Server.Transfer("../NotFound.aspx");
                }
            }
            catch (ArgumentException exception)
            {
                _errorLabel.Text = exception.Message;
            }
            catch (FormatException exception)
            {
                _errorLabel.Text = exception.Message;
            }
            catch (MockException exception)
            {
                _errorLabel.Text = exception.ExceptionMessage;
            }
        }

        private void Update()
        {
            try
            {
                object queryUsername = Request.QueryString["uname"];
                object queryRole = Request.QueryString["rname"];
                AbstractEntity source = null;
                if (queryUsername != null && queryRole != null)
                {
                    string username = queryUsername.ToString();
                    string rolename = queryRole.ToString().ToLower(CultureInfo.CurrentCulture);

                    if (rolename == "admin")
                    {
                        source = _userBusiness.GetByUserName(username);
                    }
                    else if (rolename == "contractor")
                    {
                        source = _contractorBusiness.GetByUserName(username);
                    }
                    else if (rolename == "client")
                    {
                        source = _clientBusiness.GetByUserName(username);
                    }
                    else if (rolename == "consultant")
                    {
                        source = _consultantBusiness.GetByUserName(username);
                    }
                }
                if (source == null)
                {
                    Server.Transfer("../NotFound.aspx");
                }
                else
                {
                    EditUserProfile.Filling(source);
                    if (source != null)
                    {
                        User user = source as User;
                        Client client = source as Client;
                        Consultant consultant = source as Consultant;
                        Contractor contractor = source as Contractor;
                        if (user != null)
                        {
                            _userBusiness.Update(user);
                        }
                        else if (client != null)
                        {
                            _clientBusiness.Update(client);
                        }
                        else if (consultant != null)
                        {
                            _consultantBusiness.Update(consultant);
                        }
                        else if (contractor != null)
                        {
                            _contractorBusiness.Update(contractor);
                        }
                    }
                    _successLabel.Text = "Edit successfully";
                }
            }
            catch (ArgumentException exception)
            {
                _errorLabel.Text = exception.Message;
            }
            catch (FormatException exception)
            {
                _errorLabel.Text = exception.Message;
            }
            catch (MockException exception)
            {
                _errorLabel.Text = exception.ExceptionMessage;
            }
        }
    }
}